from django.core.paginator import Paginator


# 对结果集分页处理
def get_page(ulist, pIndex, num=5):
    pIndex = int(pIndex)
    page = Paginator(ulist, num)
    maxpages = page.num_pages
    if pIndex > maxpages:
        pIndex = maxpages
    if pIndex < 1:
        pIndex = 1
    list2 = page.page(pIndex)
    plist = page.page_range
    return list2, plist, pIndex, maxpages


# 获取查询集，拼接status参数，获取关键字参数
def params_status(model, request):
    queryset_list = model.objects.filter(status__lt=9)

    mywhere = []
    status = request.GET.get('status', '')
    if status != '':
        queryset_list = queryset_list.filter(status=status)
        mywhere.append("status=" + status)

    kw = request.GET.get("keyword", None)
    return queryset_list, mywhere, kw
